/*
 *  Copyright 2001-2004 The Apache Software Foundation
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package ar.uba.fi.posgrado.common.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang.time.DateUtils;

import ar.uba.fi.posgrado.common.exception.PosgradoRuntimeException;

/**
 * Codigo comun.<br>
 * 
 * @author nicolas.gonzalez
 * @author pablo.tortorella
 */
public class CommonUtils {

	public final static String DATE_FORMAT = "dd/MM/yyyy";

	/**
	 * Convierte un {@link Set set} a {@link List list}
	 * 
	 * @param set
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static List setToList(Set set) {
		List response = new ArrayList(set);

		return response;
	}

	/**
	 * Convierte un {@link List list} a {@link Set set}
	 * 
	 * @param set
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static Set listToSet(List list) {
		Set response = new HashSet(list);

		return response;
	}

	/**
	 * Convierte a fecha a partir de un String.<br>
	 * El formato de la fecha es SIEMPRE {@link #DATE_FORMAT}
	 * 
	 * @param date
	 * @return
	 * @throws PosgradoRuntimeException
	 */
	public static Date toDateFromStringStandard(String date)
			throws PosgradoRuntimeException {
		SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);

		try {
			return sdf.parse(date);
		} catch (ParseException e) {
			throw new PosgradoRuntimeException(
					"Excepcion convirtiendo fechas. Error inesperado.", e);
		}
	}

	/**
	 * Formatea un {@link Date} a String.<br>
	 * No arroja exception.<br>
	 * 
	 * @param date
	 * @return
	 */
	public static String formatDate(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);

		try {
			return sdf.format(date);
		} catch (RuntimeException e) {
			return "<FECHA-INVALIDA>";
		}
	}

	/**
	 * Agrega amount meses a la fecha.<br>
	 * @param date
	 * @param amount
	 * @return
	 * @throws IllegalArgumentException si hay algun problema con la fecha
	 */
	public static Date addMonths(Date date, int amount) {
	    return DateUtils.addMonths(date, amount);
	}

    /**
     * Agrega amount dias a la fecha.<br>
     * @param date
     * @param amount
     * @return
     * @throws IllegalArgumentException si hay algun problema con la fecha
     */
    public static Date addDays(Date date, int amount) {
        return DateUtils.addDays(date, amount);
    }

}
